package com.flying.cattle.entity;

import lombok.Builder;
import lombok.Data;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.web.reactive.socket.WebSocketSession;
import reactor.core.publisher.FluxSink;

import java.io.Serializable;

@Data
@Builder
public class WebSocketSender implements MessageListener, Serializable {
    private static final long serialVersionUID = -4036761893992994211L;
	private WebSocketSession session;
    private FluxSink sink;


    public WebSocketSender(WebSocketSession session, FluxSink sink) {
        this.session = session;
        this.sink = sink;
    }

    public WebSocketSender() {
    }


    public void sendData(String data) {
        sink.next(session.textMessage(data));
    }


    @Override
    public void onMessage(Message message, byte[] pattern) {
        String msg = new String(message.getBody());
        sink.next(session.textMessage(msg));
    }
}
